﻿using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;


namespace sqlcmd
{
	public class SettingsReader
	{
		private static string m_fileName = "Settings.xml";
		private static string currentDir = System.IO.Directory.GetCurrentDirectory();
		private static Uri exeDir = new Uri(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase));


		public static Settings Read()
      {
			if (File.Exists(m_fileName))
			{
				return ReadFile(m_fileName);
			}
			else if (File.Exists(Path.Combine(currentDir, m_fileName)))
			{
				return ReadFile(Path.Combine(currentDir, m_fileName));
			}
			else if (File.Exists(Path.Combine(exeDir.LocalPath,m_fileName)))
			{
				return ReadFile(Path.Combine(exeDir.LocalPath, m_fileName));
			}
			string msg = string.Format("No settings.xml file found in\n{0} or {1}", currentDir, exeDir);
			throw new ApplicationException(msg);
      }

		private static Settings ReadFile(string path)
		{
			Stream s = new FileStream(path, FileMode.Open, FileAccess.Read);
			XmlSerializer ser = new XmlSerializer(typeof(Settings));
			return (Settings)ser.Deserialize(s);
		}
		public static void CreateFile(Settings set)
		{
			if (!File.Exists(m_fileName))
			{
				Stream s = new FileStream(m_fileName, FileMode.CreateNew, FileAccess.ReadWrite);
				XmlSerializer ser = new XmlSerializer(set.GetType());
				ser.Serialize(s, set);
			}
		}
	}

	[XmlInclude(typeof(DBType))]
	public class Settings
	{
		public string Description = "Sqlcmd Settings file";
		public int CommandTimeout = 300;

		private DBType[] m_dbTypes = new DBType[0];
		public DBType[] DBTypes
		{
			get { return m_dbTypes; }
			set { m_dbTypes = value; }
		}

		public void AddDBType(DBType dt)
		{
			Array.Resize<DBType>(ref m_dbTypes, m_dbTypes.Length + 1);
			m_dbTypes[m_dbTypes.Length - 1] = dt;
		}

		public DBType GetDBType(string name)
		{
			for (int i = 0; i < m_dbTypes.Length; i++)
			{
				if (name.ToLower() == m_dbTypes[i].Name.ToLower())
				{
					return m_dbTypes[i];
				}
			}
			return null;
		}
	}
	[XmlInclude(typeof(DBProperties))]
	public class DBProperties
	{
		private string m_ConnectionClass;
		public string ConnectionClass
		{
			get { return m_ConnectionClass; }
			set { m_ConnectionClass = value; }
		}
		private string m_ConnectionString;
		public string ConnectionString
		{
			get { return m_ConnectionString; }
			set { m_ConnectionString = value; }
		}
		private string m_DataAdapterClass;
		public string DataAdapterClass
		{
			get { return m_DataAdapterClass; }
			set { m_DataAdapterClass = value; }
		}
	}

	public class DBType
	{
		private string m_example;
		public string Example
		{
			get { return m_example; }
			set { m_example = value; }
		}

		private string m_name;
		public string Name
		{
			get { return m_name; }
			set { m_name = value; }
		}

		private DBProperties m_properties = new DBProperties();

		public DBProperties Properties
		{
			get { return m_properties; }
			set { m_properties = value; }
		}

		private string m_initString;
		public string InitString
		{
			get { return m_initString; }
			set { m_initString = value; }
		}
	}

}
